/*  This file is part of Basepairs, an application for calculations on
    DNA Sequence polymorphism.
    Copyright (C) 2013 Kim Lindgren

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/


package basepairs;

public class Codon {
    static String[] Codons = {"TTT", "TTC", "TTA", "TTG", "CTT", "CTC", "CTA", "CTG",
                       "ATT", "ATC", "ATA", "ATG", "GTT", "GTC", "GTA", "GTG",
                       "TCT", "TCC", "TCA", "TCG", "CCT", "CCC", "CCA", "CCG",
                       "ACT", "ACC", "ACA", "ACG", "GCT", "GCC", "GCA", "GCG",
                       "TAT", "TAC", "TAA", "TAG", "CAT", "CAC", "CAA", "CAG",
                       "AAT", "AAC", "AAA", "AAG", "GAT", "GAC", "GAA", "GAG",
                       "TGT", "TGC", "TGA", "TGG", "CGT", "CGC", "CGA", "CGG",
                       "AGT", "AGC", "AGA", "AGG", "GGT", "GGC", "GGA", "GGG"};
    static String[] Aminos = {"PHE", "PHE", "LEU", "LEU", "LEU", "LEU", "LEU", "LEU",
                       "ILE", "ILE", "ILE", "MET", "VAL", "VAL", "VAL", "VAL",
                       "SER", "SER", "SER", "SER", "PRO", "PRO", "PRO", "PRO",
                       "THR", "THR", "THR", "THR", "ALA", "ALA", "ALA", "ALA",
                       "TYR", "TYR", "XXX", "XXX", "HIS", "HIS", "GLN", "GLN",
                       "ASN", "ASN", "LYS", "LYS", "ASP", "ASP", "GLU", "GLU",
                       "CYS", "CYS", "XXX", "TRP", "ARG", "ARG", "ARG", "ARG",
                       "SER", "SER", "ARG", "ARG", "GLY", "GLY", "GLY", "GLY"};
    
    public static boolean isSynonymous(String c1, String c2){
        if(c1.length() != 3){
            return false;
        }
        else if(c2.length() != 3){
            return false;
        }   
        
        if(c1.substring(0,2).equals(c2.substring(0,2))){
            return true;
        }
        
        int index_c1 = indexOf(c1);
        int index_c2 = indexOf(c2);
        
        if(index_c1 == -1){
            System.out.println("ERROR: CAN NOT FIND CODON: " + c1 + ", IN DATABASE");
            return false;
        }
        if(index_c2 == -1){
            System.out.println("ERROR: CAN NOT FIND CODON: " + c2 + ", IN DATABASE");
            return false;
        }
        
        if(Aminos[index_c1].equals(Aminos[index_c2])){
            return true;
        }
        
        return false;
    }
    
    public static String getAmino(String codon){
        int index = indexOf(codon);
        
        if(index == -1){
            System.out.println("ERROR: CAN NOT FIND CODON: " + codon + ", IN DATABASE");
            return "XXX";
        }
        
        return Aminos[index];
    }
    
    static int indexOf(String codon){
        for(int i = 0; i < Codons.length; i++){
            if(Codons[i].equals(codon)){
                return i;
            }
        }
        
        return -1;
    }
}
